package com.wlyuan.index.domain.source.valueobj;

import com.wlyuan.core.marker.INamed;
import com.wlyuan.core.marker.NamedMap;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.RequiredArgsConstructor;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
 * @author yuanjie
 */
@RequiredArgsConstructor
@Getter
@AllArgsConstructor
public enum QuotationStatus implements INamed<Integer> {
    DRAFT(Status.DRAFT, "草稿", false),
    SUBMITTED(Status.SUBMITTED, "已发送", false),
    @Deprecated
    AUDITING(Status.AUDITING, "待审核", false), // FIXME: 这个状态实际不存在，但需要调整报价系统，先兼容
    AGREED(Status.AGREED, "已同意", false),
    REJECTED(Status.REJECTED, "已拒绝", false),
    BARGAIN(Status.BARGAIN, "议价", false),
    RECALLED(Status.RECALLED, "已撤回", true),
    RETURNED(Status.RETURNED, "已退回", true),
    DISCARDED(Status.DISCARDED, "已作废", true),
    BARGAINING(Status.BARGAINING, "议价中", false),
    CLOSED(Status.CLOSED, "已关闭", false),
    STOPPED(Status.STOPPED, "已终止", false, "过关账期自动终止"),// FIXME: 与主动区别开，需要报价系统同步修改
    WAITING(Status.WAITING, "待报价", true); // 生成的状态，表示处于待报价状态

    private final Integer value;
    private final String name;
    private final boolean canQuote;
    private String remarks;


    private interface Status {
        Integer DRAFT = 0;
        Integer SUBMITTED = 1;
        Integer AGREED = 2;
        Integer REJECTED = 3;
        Integer BARGAIN = 8;
        Integer AUDITING = 11;
        Integer RECALLED = 12;
        Integer RETURNED = 13;
        Integer DISCARDED = 14;
        Integer BARGAINING = 18;
        Integer CLOSED = 32;
        Integer STOPPED = 40;
        Integer WAITING = 80;
    }

    public int getPriority() {
        if (REVERSED.contains(this)) {
            return -this.value;
        }
        return this.value;
    }


    private static final List<QuotationStatus> REVERSED = Arrays.asList(RECALLED, RETURNED, DISCARDED);
    private static final Map<Integer, QuotationStatus> STATUSES = new NamedMap<>(QuotationStatus.values());

    public static QuotationStatus valueOf(Integer value) {
        return STATUSES.get(value);
    }

    public static boolean isCanQuote(Integer value) {
        return Optional.ofNullable(value)
                .map(QuotationStatus::isCanQuote)
                .orElse(true);
    }
}